package com.lyk.coursearrange.common.core.base.result;

import com.lyk.coursearrange.common.core.base.code.CodeEnum;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局统一返回结果类
 *
 * @author 然子哥
 */
@Data
public class R<T> {

    /**
     * 返回提示
     **/
    private Boolean success;

    /**
     * 状态码
     **/
    private Integer code;

    /**
     * 返回消息
     **/
    private String message;

    /**
     * 返回数据
     **/
    private Map<String, Object> data = new HashMap<String, Object>();


    public R() {
    }

    /**
     * 状态类型
     */
    public enum Type {
        /**
         * 成功
         */
        SUCCESS(0),
        /**
         * 警告
         */
        WARN(301),
        /**
         * 错误
         */
        ERROR(500);
        private final int value;

        Type(int value) {
            this.value = value;
        }

        public int value() {
            return this.value;
        }
    }

    public static R ok() {
        R r = new R();
        r.setSuccess(CodeEnum.SUCCESS.getSuccess());
        r.setCode(CodeEnum.SUCCESS.getCode());
        r.setMessage(CodeEnum.SUCCESS.getMessage());
        return r;
    }



    public static R error() {
        R r = new R();
        r.setSuccess(CodeEnum.UNKNOWN_REASON.getSuccess());
        r.setCode(CodeEnum.UNKNOWN_REASON.getCode());
        r.setMessage(CodeEnum.UNKNOWN_REASON.getMessage());
        return r;
    }

    public static <T> R<T> error(String message) {
        R<T> r = new R<>();
        r.setSuccess(CodeEnum.UNKNOWN_REASON.getSuccess());
        r.setCode(CodeEnum.UNKNOWN_REASON.getCode());
        r.setMessage(message);
        return r;
    }

    public static R setResult(CodeEnum resultCodeEnum) {
        R r = new R();
        r.setSuccess(resultCodeEnum.getSuccess());
        r.setCode(resultCodeEnum.getCode());
        r.setMessage(resultCodeEnum.getMessage());
        return r;
    }

    public R success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }

    /**
     * 初始化一个新创建的 R 对象
     *
     * @param type 状态类型
     * @param msg 返回内容
     * @param data 数据对象
     */
    /*public R(Type type, String msg, Object data)
    {
        super.put(code, type.value);
        super.put(msg, msg);
        if (StringUtils.isNotNull(data))
        {
            super.put(data, data);
        }
    }*/
}

